<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务商名称"
        >
          <a-input placeholder="服务商名称"
                  v-decorator="[ 'name', {rules: [{ required: true,len:'3',max:'3', message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="厂商"
        >
          <a-select :disabled="isDisabled"
                    showSearch
                    optionFilterProp="children"
                    :filterOption="filterOption"
                    v-decorator="[ 'manufacturerId', {rules: [{ required: true, message: '请选择。。。' }]} ]">
            <a-select-option v-for="item in businessList" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="APPID"
        >
          <a-input placeholder="APPID"
                   v-decorator="[ 'wxAppId', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商户号"
        >
          <a-input placeholder="商户号"
                   v-decorator="[ 'wxMchId', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商户密匙"
        >
          <a-input placeholder="商户密匙"
                   v-decorator="[ 'wxMchKey', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="联系电话"
        >
          <a-input placeholder="联系电话"
                   v-decorator="[ 'phone', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上报费率"
        >
          <a-input placeholder="上报费率"
                   v-decorator="[ 'reportRate', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { addOrUpdate } from '@/api/businessService/business'
  import { selectAll} from '@/api/companyService/company'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        companyList:[],
        defaultCompany:[],
        isDisabled: false,
        businessList:[]
      }
    },
    //--############  data() ###############-->

    mounted() {
      selectAll().then(res => {
        this.businessList = res.result
      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      edit(record) {
        console.log('edit',record)
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'name','manufacturerId', 'wxAppId', 'wxMchId','wxMchKey','reportRate','phone'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },
      handleOk() {
        const _this = this
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id;
            _this.confirmLoading = true
            addOrUpdate(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
